package com.runjian.rundo.iot.agent.iec104.parser.frame.control.base;

import cn.hutool.core.util.ByteUtil;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;
import com.runjian.rundo.iot.agent.iec104.parser.frame.PointRemoteControl;

import com.runjian.rundo.iot.agent.iec104.parser.frame.RemoteDebug;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;

import java.nio.charset.Charset;

/**
 * 遥控
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public abstract class YkControl extends BaseControlFrame{


    @Override
    public Short getSe() {
        Short se = 0;
        Object obj = this.getInfoElements().get(0).getValue();
        if (obj instanceof PointRemoteControl) {
            se = (short)(((PointRemoteControl)obj).isType()?1:0);
        }
        return se;
    }

    @Override
    public void setSe(Short se) {
        this.getInfoElements().forEach(infoElement -> {
            if (infoElement.getValue() instanceof PointRemoteControl) {
                ((PointRemoteControl)infoElement.getValue()).setType(se==1?Boolean.TRUE:Boolean.FALSE);
            }else if (infoElement.getValue() instanceof RemoteDebug){
                ((RemoteDebug)infoElement.getValue()).setSe(se==1?Boolean.TRUE:Boolean.FALSE);
            }
        });
    }

    /**
     * 写入byteBuf
     * @param assembleByteBuf 字对缓存对象
     * @param infoElement 信息对象
     */
    public void writeInfoContent(ByteBuf assembleByteBuf, InfoElement<Object> infoElement) {
        if(infoElement.getValue() instanceof PointRemoteControl) {
            PointRemoteControl pointRemoteControl=(PointRemoteControl)infoElement.getValue();
            assembleByteBuf.writeCharSequence(pointRemoteControl.getStatusStr(), Charset.forName("UTF-8"));
        }else if (infoElement.getValue() instanceof RemoteDebug){
            RemoteDebug remoteDebug=(RemoteDebug)infoElement.getValue();
            assembleByteBuf.writeCharSequence(remoteDebug.toHex(), Charset.forName("UTF-8"));
        }
    }

}
